import { _decorator, Component, debug, Label, Node, v2, v3, Vec2 } from 'cc';
import { MapZone } from '../entity/MapZone';
import { Village } from '../entity/Village';
import { buildingType, raceName } from '../settings/const';
import { UserInfo } from '../common/UserInfo';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Transport } from '../village/Transport';
import { ArmyIndex } from '../army/ArmyIndex';
import { BattleLayout } from '../army/BattleLayout';
import AllyApi from '../api/AllyApi';
import { Ally, AllyOperateType } from '../entity/Ally';
import { BManager } from '../entity/BManager';
import { WorldController } from '../common/WorldController';
const { ccclass, property } = _decorator;

@ccclass('PanelVillage')
export class PanelVillage extends Component {
    @property(Label)
    race: Label;//种族
    @property(Label)
    allyName: Label;//联盟
    @property(Label)
    userName: Label; //所有者
    @property(Label)
    people: Label; // 居民数
    @property(Node)
    fight: Node
    @property(Node)
    invent: Node
    @property(Node)
    enter: Node
    @property(Node)
    transport: Node


    private villageData: Village
    private zoneData:MapZone

    start() {
        this.enter.on(Node.EventType.TOUCH_END, this.onEnter, this)
        this.fight.on(Node.EventType.TOUCH_END, this.onFight, this)
        this.transport.on(Node.EventType.TOUCH_END, this.onTransport, this)
        this.invent.on(Node.EventType.TOUCH_END, this.onInvent, this)
    }

    setData(zone: MapZone, data: Village) {
        this.zoneData = zone
        this.villageData = data
        this.hideAllBtn()

        this.race.string = raceName[data.user.uInfo.race]
        this.allyName.string = data.user.allyName
        this.userName.string = data.user.uInfo.name
        this.people.string = data.people + ''
        
        if (data.uid == UserInfo.instance.villageData.uid) {
            // 我的村庄
            // 要判断是否当前村庄
            if (zone.zoneId == UserInfo.instance.villageData.curVid) {
                this.enter.active = true
                this.enter.position = v3(0, -136, 0)
            } else {
                this.enter.active = true
                this.transport.active = true
                this.enter.position = v3(-76, -136, 0)
                this.transport.position = v3(76, -136, 0)
            }
        } else {
            // 他人的村庄
            let ally = Ally.getMyAlly()
            if (ally && ally.isLeader) {
                this.fight.active = true
                this.invent.active = true
                this.fight.position = v3(-76, -136, 0)
                this.invent.position = v3(76, -136, 0)
            } else {
                this.fight.active = true
                this.fight.position = v3(0, -136, 0)
            }

        }
    }

    hideAllBtn() {
        this.fight.active = false
        this.invent.active = false
        this.enter.active = false
        this.transport.active = false
    }

    async onEnter() {
        if(WorldController.instance){
            WorldController.instance.enabled = false
        }
        
        await UserInfo.instance.changeVillage(this.villageData.zoneId)
        UIManager.instance.loadScene('village')
    }
    async onFight() {
        if(!this.zoneData.isOverThreeDay){
            UIManager.instance.modal('提示','该玩家处在新手保护期', false)
            return
        }
       
        let myAlly = Ally.getMyAlly()
       
        if(myAlly && myAlly.AllyId == this.villageData.user.allyId){
            UIManager.instance.modal('提示','该玩家是您的盟友，不要挑衅', false)
            return
        }
        
        let ui = await UIManager.instance.openUI(PrefabEnum.ArmyIndex)
        let armyIndex = ui.getComponent(ArmyIndex)
        armyIndex.onBattle(this.villageData)
    }
    async onTransport() {
        let bm = BManager.getBManageById(buildingType.B17_Market)
        
        if(!bm){
            UIManager.instance.modal('提示',`您还没有建造市场，无法运输物资`, false)
            return
        }
        let ui = await UIManager.instance.openUI(PrefabEnum.Transport)
        ui.getComponent(Transport).onSelectVillage(this.villageData)

    }
    async onInvent() {
        if(this.villageData.user.allyId != -1){
            UIManager.instance.modal('提示',`该玩家已加入${this.villageData.user.allyName}联盟`, false)
            return
        }

        try {
            await UIManager.instance.modal('提示', `您确定邀请${this.villageData.user.uInfo.name}加入联盟吗？`,)
            await UIManager.instance.showLoading()
            let res = await AllyApi.Execute({
                op: AllyOperateType.Invite,
                uid: this.villageData.uid
            })
            UIManager.instance.closeLoading()
            UIManager.instance.modal('提示', `已经对${this.villageData.user.uInfo.name}发出了邀请`,)
        } catch (error) {

        }
    }
}


